#include "test.h"
#include "zuoye_1.h"
#include "zuoye_2.h"
/*********************************btn***********************************/
// 回调函数
static void event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked");
    } else if(code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("Toggled");
    }
}

void lv_bling_btn_1(void)
{
    lv_obj_t * label;

    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL);
    lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -40);

    label = lv_label_create(btn1);
    lv_label_set_text(label, "Button");
    lv_obj_center(label);

    lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn2, event_handler, LV_EVENT_ALL, NULL);
    lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 40);
    lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
    lv_obj_set_height(btn2, LV_SIZE_CONTENT);

    label = lv_label_create(btn2);
    lv_label_set_text(label, "Toggle");
    lv_obj_center(label);
}

/*
    4个按键的用法
*/
// 按键1回调函数
static void event_handler1(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    char * user_data     = (char *)lv_event_get_user_data(e); // 传的字符串

    if(code == LV_EVENT_CLICKED)
        // LV_LOG_USER("Clicked");
        printf("anjian1:%s\n", user_data);
}
// 按键2回调函数
static void event_handler2(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
        // LV_LOG_USER("Clicked");
        printf("anjian2\n");
}
// 按键3回调函数
static void event_handler3(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)
        // LV_LOG_USER("Clicked");
        printf("anjian3\n");
}
// 按键4回调函数
static void event_handler4(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)
        // LV_LOG_USER("Clicked");
        printf("anjian4\n");
}

// 作业1:4个按键的demo
void lv_zuoye1_btn_1(void)
{
    lv_obj_t * label;

    /*btn1的按键*/
    // 创建一个按键的对象
    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    // 添加一个按键的时间
    // lv_obj_add_event_cb(btn1, event_handler1, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(btn1, event_handler1, LV_EVENT_ALL, (void *)"10.1happy");
    // 添加按键的位置(可以自定义按照代码里面的宏LV_ALIGN_CENTER)
    lv_obj_align(btn1, LV_ALIGN_CENTER, -80, -80); // 左上

    // 创建一个label标签的对象(这个label是依附在btn1上面的label)
    label = lv_label_create(btn1);
    // 设置label的文本信息(暂时先用英文)
    lv_label_set_text(label, "anjian1");
    // 设置文本的位置
    lv_obj_center(label);

    /*btn2的按键*/
    lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn2, event_handler2, LV_EVENT_ALL, NULL);
    lv_obj_align(btn2, LV_ALIGN_CENTER, -80, 80); // 左下
    // lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
    // lv_obj_set_height(btn2, LV_SIZE_CONTENT);

    label = lv_label_create(btn2);
    lv_label_set_text(label, "anjian2");
    lv_obj_center(label);

    /*btn3的按键*/
    lv_obj_t * btn3 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn3, event_handler3, LV_EVENT_ALL, NULL);
    lv_obj_align(btn3, LV_ALIGN_CENTER, 80, -80); // 右上

    label = lv_label_create(btn3);
    lv_label_set_text(label, "anjian3");
    lv_obj_center(label);

    /*btn4的按键*/
    lv_obj_t * btn4 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn4, event_handler4, LV_EVENT_ALL, NULL);
    lv_obj_align(btn4, LV_ALIGN_CENTER, 80, 80); // 右下

    label = lv_label_create(btn4);
    lv_label_set_text(label, "anjian3");
    lv_obj_center(label);
}

// 图标按钮在(x,y)的坐标位置 放了一个w宽度*h高度大小的按键
lv_obj_t * lv_bling_imgbtn(int x, int y, int w, int h, char * pic_path)
{
    /*Darken the button when pressed and make it wider*/
    // 添加按键的样式表，使他有一个动态的效果
    static lv_style_t style_pr; // 样式表
    lv_style_init(&style_pr);
    lv_style_set_img_recolor_opa(&style_pr, LV_OPA_30);
    lv_style_set_img_recolor(&style_pr, lv_color_black());
    lv_style_set_transform_height(&style_pr, 20);

    /*Create an image button*/
    lv_obj_t * imgbtn = lv_imgbtn_create(lv_scr_act());
    if(imgbtn == NULL) {
        LV_LOG_ERROR("lv_imgbtn_create fail:"); // 输出错误提示
        return NULL;
    }

    // 设置按键的大小
    lv_obj_set_size(imgbtn, w, h);
    lv_imgbtn_set_src(imgbtn, LV_IMGBTN_STATE_RELEASED, NULL, pic_path, NULL);
    // lv_obj_add_style(imgbtn1, &style_def, 0);
    lv_obj_add_style(imgbtn, &style_pr, LV_STATE_PRESSED);

    //(x和h按键偏移的位置)
    lv_obj_align(imgbtn, LV_ALIGN_TOP_LEFT, x, y);

    return imgbtn;
}
// 按键的回调函数
static void video_btn_event_callback(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    // 点击按键之后所处理的事情在这个if语句里面实现
    if(code == LV_EVENT_CLICKED) {
        printf("video.png\n");
        // LV_LOG_USER("Clicked");
    }
}

// 显示图像按钮
void test_btn_image()
{
    // 3.在主界面中创建音乐与视频图标(注意：要使用开发板里面的绝对路径 图片的大小是200*200)
    lv_obj_t * video = lv_bling_imgbtn(100, 100, 200, 200, "S:/IOT/bling/200.png");
    lv_obj_add_event_cb(video, video_btn_event_callback, LV_EVENT_CLICKED, NULL);
}

/*********************************label***********************************/
void lv_bling_label_1(void)
{
    lv_obj_t * label = lv_label_create(lv_scr_act());
    // 设置文本内容
    lv_label_set_text(label, "Hello, LVGL!");

    // 使能字体的颜色
    // lv_label_set_recolor(label, true);
    // lv_label_set_text(label,"#0000ff led#"); //设置文本颜色的格式

    // 设置label的大小：
    lv_obj_set_width(label, 300); // 设置宽度为300px
    lv_obj_set_height(label, 50); // 设置高度为50px

    // 设置字体的大小
    lv_obj_set_style_text_font(label, &lv_font_montserrat_30, 0);

    // 设置标签的位置
    lv_obj_set_pos(label, 100, 100);
}

// 滚动字体的效果
void lv_bling_label_2(void)
{
    lv_obj_t * label = lv_label_create(lv_scr_act());
    // 设置标签的模式为滚动的模式
    lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR); /*Circular scroll*/
    // 设置label显示的宽度大小
    lv_obj_set_width(label, 800);
    lv_label_set_recolor(label, true);
    // 注意：字体显示的内容要超过你自定义的区域大小
    lv_label_set_text(
        label,
        "#0000ff It is a circularly scrolling text.dsakjdhaskjdhkjashdkahdakjshdkasjdhjkshdkj#"); // 设置文本颜色的格式
    lv_obj_set_style_text_font(label, &lv_font_montserrat_40, 0);
    // lv_label_set_text(label, "It is a circularly scrolling text. ");
    lv_obj_align(label, LV_ALIGN_TOP_LEFT, 0, 240);
}

/*********************************img***********************************/
void lv_bling_image_1(void)
{
    /*
        如何实现jpg图片
        显示jpg在坐标(0,0)的位置放了一张200_200.jpg图
    */
    // 实例化一个img的对象
    lv_obj_t * img1 = lv_img_create(lv_scr_act());
    // 将图片放在lcd(0,0)的坐标位置
    lv_obj_set_pos(img1, 0, 0);
    // 设置图片的路劲(注意图片的路径要和开发板里面的路径一致)
    lv_img_set_src(img1, "S:/IOT/bling/jige.jpg");
    printf("show jpg200\n");
}

/*********************************gif***********************************/
void lv_bling_gif_1(void)
{
    /*
        如何实现gif动态图的控件
    */
    // 显示gif在坐标(200,0)的位置放了一张gif图
    // 实例化一个gif的对象
    lv_obj_t * gif1 = lv_gif_create(lv_scr_act());
    // 设置gif图的坐标位置
    lv_obj_set_pos(gif1, 0, 0);
    // lv_obj_align(gif1, LV_ALIGN_RIGHT_MID, -20, 0); //在起始坐标的位置左移20
    // 设置gif图的路径
    lv_gif_set_src(gif1, "S:/IOT/bling/kawa.gif");
    printf("show gif\n");
}

/*********************************msgbox***********************************/
static void event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_current_target(e);
    LV_LOG_USER("Button %s clicked", lv_msgbox_get_active_btn_text(obj));

    // 如果是lv_msgbox_get_active_btn_text(obj) == "apply" 表示确定
    // 如果是lv_msgbox_get_active_btn_text(obj) == "close" 表示不确定
    // 用strcmp函数
    const char * ret_str = lv_msgbox_get_active_btn_text(obj);
    if(strcmp(ret_str, "Apply") == 0) printf("按确定按键之后需要处理的事情\n");
    if(strcmp(ret_str, "Close") == 0) printf("按取消按键之后需要处理的事情\n");
}

void lv_bling_msgbox_1(void)
{
    static const char * btns[] = {"Apply", "Close", ""};
    /*
        "Hello"是title标签
        "This is a message box with two buttons."是文本信息
    */
    lv_obj_t * mbox1 = lv_msgbox_create(NULL, "Hello", "This is a message box with two buttons.", btns, true);
    lv_obj_add_event_cb(mbox1, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_center(mbox1);
}

/*********************************list列表***********************************/
static lv_obj_t * list1; // 回调函数和主函数都要使用所以定义成全局变量

static void event_list_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj       = lv_event_get_target(e);
    if(code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked: %s", lv_list_get_btn_text(list1, obj));
    }
}

void lv_bling_list_1(void)
{
    /*Create a list*/
    list1 = lv_list_create(lv_scr_act());
    lv_obj_set_size(list1, 180, 220);
    lv_obj_center(list1);

    /*Add buttons to the list*/
    lv_obj_t * btn;

    lv_list_add_text(list1, "File");
    btn = lv_list_add_btn(list1, LV_SYMBOL_FILE, "New");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_DIRECTORY, "Open");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_SAVE, "Save");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_CLOSE, "Delete");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_EDIT, "Edit");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);

    lv_list_add_text(list1, "Connectivity");
    btn = lv_list_add_btn(list1, LV_SYMBOL_BLUETOOTH, "Bluetooth");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_GPS, "Navigation");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_USB, "USB");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_BATTERY_FULL, "Battery");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);

    lv_list_add_text(list1, "Exit");
    btn = lv_list_add_btn(list1, LV_SYMBOL_OK, "Apply");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_CLOSE, "Close");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
}

/*********************************keyboard键盘***********************************/
// static void ta_event_cb(lv_event_t * e)
// {
//     lv_event_code_t code = lv_event_get_code(e);
//     lv_obj_t * ta        = lv_event_get_target(e);
//     lv_obj_t * kb        = lv_event_get_user_data(e);
//     if(code == LV_EVENT_FOCUSED) {
//         lv_keyboard_set_textarea(kb, ta);
//         lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
//     }

//     if(code == LV_EVENT_DEFOCUSED) {
//         lv_keyboard_set_textarea(kb, NULL);
//         lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
//     }
//     // const char * text = lv_textarea_get_text(ta); // 获取当前输入的文本
//     // LV_LOG_USER("输入内容: %s", text);
// }

// void lv_bling_keyboard_1(void)
// {
//     /*Create a keyboard to use it with an of the text areas*/
//     lv_obj_t * kb = lv_keyboard_create(lv_scr_act());

//     /*Create a text area. The keyboard will write here*/
//     lv_obj_t * ta;
//     ta = lv_textarea_create(lv_scr_act());
//     lv_obj_align(ta, LV_ALIGN_TOP_LEFT, 10, 10);
//     lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, kb);
//     lv_textarea_set_placeholder_text(ta, "Hello");
//     lv_obj_set_size(ta, 140, 80); // 140*80

//     ta = lv_textarea_create(lv_scr_act());
//     lv_obj_align(ta, LV_ALIGN_TOP_RIGHT, -10, 10);
//     lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, kb);
//     lv_obj_set_size(ta, 140, 80);

//     lv_keyboard_set_textarea(kb, ta);
// }

/*********************************text文本框***********************************/
static void textarea_event_handler(lv_event_t * e)
{
    lv_obj_t * ta = lv_event_get_target(e);
    LV_LOG_USER("Enter was pressed. The current text is: %s", lv_textarea_get_text(ta));
}

static void btnm_event_handler(lv_event_t * e)
{
    lv_obj_t * obj   = lv_event_get_target(e);
    lv_obj_t * ta    = lv_event_get_user_data(e);
    const char * txt = lv_btnmatrix_get_btn_text(obj, lv_btnmatrix_get_selected_btn(obj));

    if(strcmp(txt, LV_SYMBOL_BACKSPACE) == 0)
        lv_textarea_del_char(ta);
    else if(strcmp(txt, LV_SYMBOL_NEW_LINE) == 0)
        lv_event_send(ta, LV_EVENT_READY, NULL);
    else
        lv_textarea_add_text(ta, txt);
}

void lv_bling_textarea_1(void)
{
    lv_obj_t * ta = lv_textarea_create(lv_scr_act());
    lv_textarea_set_one_line(ta, true);
    lv_obj_align(ta, LV_ALIGN_TOP_MID, 0, 10);
    lv_obj_add_event_cb(ta, textarea_event_handler, LV_EVENT_READY, ta);
    lv_obj_add_state(ta, LV_STATE_FOCUSED); /*To be sure the cursor is visible*/

    static const char * btnm_map[] = {
        "1", "2", "3", "\n", "4", "5", "6", "\n", "7", "8", "9", "\n", LV_SYMBOL_BACKSPACE, "0", LV_SYMBOL_NEW_LINE,
        ""};

    lv_obj_t * btnm = lv_btnmatrix_create(lv_scr_act());
    lv_obj_set_size(btnm, 200, 150); // 设置键盘的大小
    lv_obj_align(btnm, LV_ALIGN_BOTTOM_MID, 0, -10);
    lv_obj_add_event_cb(btnm, btnm_event_handler, LV_EVENT_VALUE_CHANGED, ta);
    lv_obj_clear_flag(btnm, LV_OBJ_FLAG_CLICK_FOCUSABLE); /*To keep the text area focused on button clicks*/
    lv_btnmatrix_set_map(btnm, btnm_map);
}

void lvgl_hezi()
{
    lv_obj_t * widget = lv_obj_create(lv_scr_act());
    lv_obj_set_size(widget, 100, 100);
    lv_obj_set_align(widget, LV_ALIGN_CENTER);

    // 1、定义一个样式变量
    static lv_style_t style;
    // 2、初始化变量
    lv_style_init(&style);
    // 3、往样式变量中设置背景颜色
    lv_style_set_bg_color(&style, lv_color_hex(0xff0000));
    // 4、将样式变量 添加到对象中
    lv_obj_add_style(widget, &style, 0);
}

void lvgl_biankuang()
{
    // 得到当前屏幕的界面对象
    lv_obj_t * parent = lv_scr_act();
    int width         = lv_obj_get_width(parent);
    int height        = lv_obj_get_height(parent);

    lv_obj_t * btn1 = lv_btn_create(parent);
    lv_obj_set_size(btn1, 100, 100);

    lv_obj_t * btn2 = lv_btn_create(parent);
    lv_obj_set_size(btn2, 100, 100);
    lv_obj_align(btn2, LV_ALIGN_RIGHT_MID, 0, 0);

    // 初始化样式
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_bg_color(&style, lv_color_hex(0xff0000));
    lv_style_set_border_width(&style, 10);
    lv_style_set_border_color(&style, lv_color_hex(0x00ff00));

    // 把设置好的样式生效于控件
    lv_obj_add_style(btn1, &style, LV_PART_MAIN);
    lv_obj_add_style(btn2, &style, LV_PART_MAIN);
}

void lvgl_backgroud()
{
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, 5);

    /*Make a gradient*/
    lv_style_set_bg_opa(&style, LV_OPA_COVER);
    lv_style_set_bg_color(&style, lv_palette_lighten(LV_PALETTE_GREY, 1));
    lv_style_set_bg_grad_color(&style, lv_palette_main(LV_PALETTE_BLUE));
    lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER);

    /*Shift the gradient to the bottom*/
    lv_style_set_bg_main_stop(&style, 128);
    lv_style_set_bg_grad_stop(&style, 192);

    /*Create an object with the new style*/
    lv_obj_t * obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(obj, &style, 0);
    lv_obj_center(obj);
}

void lvgl_shadow()
{
    static lv_style_t style;
    lv_style_init(&style);

    /*Set a background color and a radius*/
    lv_style_set_radius(&style, 5);
    lv_style_set_bg_opa(&style, LV_OPA_COVER);
    lv_style_set_bg_color(&style, lv_palette_lighten(LV_PALETTE_GREY, 1));

    /*Add a shadow*/
    lv_style_set_shadow_width(&style, 55);
    lv_style_set_shadow_color(&style, lv_palette_main(LV_PALETTE_BLUE));
    //    lv_style_set_shadow_ofs_x(&style, 10);
    //    lv_style_set_shadow_ofs_y(&style, 20);

    /*Create an object with the new style*/
    lv_obj_t * obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(obj, &style, 0);
    lv_obj_center(obj);
}
#if 0
#include "lv_font_source_han_sans_bold.h" //中文字体的头文件
static void event_font_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_PRESSED) {
        printf("ok\n");
    }
}
void font_test()
{
    /*
    显示中文字的说明:要添加 chinese_ziku.c lv_font_source_han_sans_bold_20.c两个文件
    */
    // 在按键上面显示中文标签
    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn1, 200, 200);
    lv_obj_set_pos(btn1, 400, 240);
    lv_obj_add_event_cb(btn1, event_font_handler, LV_EVENT_PRESSED, NULL);

    lv_obj_t * wendu = lv_label_create(btn1);
    lv_obj_align(wendu, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(wendu, "上一张");
    // 20字体的大小
    // lv_obj_set_style_text_font(wendu,&lv_font_source_han_sans_bold_20,0);
    // 20字体的大小粗体
    lv_obj_set_style_text_font(wendu, &chinese_ziku, 0);

    // 直接显示中文label标签
    lv_obj_t * label = lv_label_create(lv_scr_act());
    // 设置文本内容
    lv_label_set_text(label, "中文好丝滑!");
    // 设置label的大小：
    lv_obj_set_width(label, 300); // 设置宽度为100px
    lv_obj_set_height(label, 50); // 设置高度为50px
    // 设置字体的大小
    lv_obj_set_style_text_font(label, &chinese_ziku, 0);
    // 设置标签的位置
    lv_obj_set_pos(label, 100, 100);
}
// 两个按键测试定时器
lv_timer_t * timer1 = NULL;
void my_timer_1(lv_timer_t * timer)
{
    /*Use the user_data*/
    uint32_t * user_data = timer->user_data;
    printf("my_timer called with user data: %d\n", *user_data);
}

static void event_handler_bnt_11(lv_event_t * e) // 按键1启动定时器
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_PRESSED) {
        printf("ok\n");
        // lv_timer_ready(timer1);  //启动定时器
        lv_timer_resume(timer1); // 恢复定时器
    }
}

static void event_handler_bnt_12(lv_event_t * e) // 按键2暂停定时器
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_PRESSED) {
        printf("no ok\n");
        // lv_timer_del(timer1); //删除定时器
        lv_timer_pause(timer1); // 暂停
    }
}

void timer_button_test()
{
    // 初始化一个定时器(每500ms执行一个函数my_timer_1)
    static uint32_t user_data = 10;
    timer1                    = lv_timer_create(my_timer_1, 500, &user_data);

    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    // 设置按键btn的size大小200*200
    lv_obj_set_size(btn1, 100, 100);
    // 设置按键btn的位置(400,240)
    lv_obj_set_pos(btn1, 600, 100);
    lv_obj_add_event_cb(btn1, event_handler_bnt_11, LV_EVENT_PRESSED, NULL);
    // 显示文字在按键btn上
    lv_obj_t * label1 = lv_label_create(btn1); // 实例化一个标签label对象
    lv_label_set_text(label1, "开定时器");     // 给标签注释
    lv_obj_set_style_text_font(label1, &chinese_ziku, 0);
    lv_obj_center(label1); // 将文本放在按键的中间

    lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
    // 设置按键btn的size大小200*200
    lv_obj_set_size(btn2, 100, 100);
    // 设置按键btn的位置(400,240)
    lv_obj_set_pos(btn2, 600, 300);
    lv_obj_add_event_cb(btn2, event_handler_bnt_12, LV_EVENT_PRESSED, NULL);
    // 显示文字在按键btn上
    lv_obj_t * label2 = lv_label_create(btn2); // 实例化一个标签label对象
    lv_label_set_text(label2, "关定时器");     // 给标签注释
    lv_obj_set_style_text_font(label2, &chinese_ziku, 0);
    lv_obj_center(label2); // 将文本放在按键的中间
    lv_obj_move_foreground(btn1);
    lv_obj_move_foreground(btn2);
}
#endif

#include "lv_font_source_han_sans_bold.h"

// ------------------------ 页面管理函数 ------------------------
void ui_load_page(void (*create_func)(void))
{
    lv_obj_clean(lv_scr_act()); // 清空当前屏幕
    create_func();              // 创建新页面
}

// ------------------------ 函数声明 ------------------------
static void ta_event_cb1(lv_event_t * e);
static void bg_event_cb(lv_event_t * e);
static void login_event_cb(lv_event_t * e);
static void register_event_cb(lv_event_t * e);
void lv_zhuce(void);
void page_home(void);
void zhuce_home(void);

// ------------------------ 全局变量 ------------------------
static lv_obj_t * kb;
static lv_obj_t * text_ta;      // 账号输入框
static lv_obj_t * pwd_ta;       // 密码输入框
static lv_obj_t * login_btn;    // 登录按钮
static lv_obj_t * register_btn; // 注册按钮
const char * user;
const char * pass;

// ------------------------ 注册页 ------------------------
void lv_zhuce(void)
{
    lv_obj_t * bg = lv_obj_create(lv_scr_act());
    lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_clear_flag(bg, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_add_event_cb(bg, bg_event_cb, LV_EVENT_CLICKED, NULL);

    // 账号输入框
    text_ta = lv_textarea_create(lv_scr_act());
    lv_textarea_set_one_line(text_ta, true);
    lv_textarea_set_password_mode(text_ta, false);
    lv_obj_set_width(text_ta, lv_pct(40));
    lv_obj_add_event_cb(text_ta, ta_event_cb1, LV_EVENT_ALL, NULL);
    lv_obj_align(text_ta, LV_ALIGN_CENTER, 0, -160);

    lv_obj_t * oneline_label = lv_label_create(lv_scr_act());
    lv_label_set_text(oneline_label, "账号");
    lv_obj_set_style_text_font(oneline_label, &chinese_ziku, 0);
    lv_obj_align_to(oneline_label, text_ta, LV_ALIGN_OUT_TOP_LEFT, 0, 0);

    // 密码输入框
    pwd_ta = lv_textarea_create(lv_scr_act());
    lv_textarea_set_text(pwd_ta, "");
    lv_textarea_set_password_mode(pwd_ta, true);
    lv_textarea_set_one_line(pwd_ta, true);
    lv_obj_set_width(pwd_ta, lv_pct(40));
    lv_obj_align(pwd_ta, LV_ALIGN_CENTER, 0, -60);
    lv_obj_add_event_cb(pwd_ta, ta_event_cb1, LV_EVENT_ALL, NULL);

    lv_obj_t * pwd_label = lv_label_create(lv_scr_act());
    lv_label_set_text(pwd_label, "密码");
    lv_obj_set_style_text_font(pwd_label, &chinese_ziku, 0);
    lv_obj_align_to(pwd_label, pwd_ta, LV_ALIGN_OUT_TOP_LEFT, 0, 0);

    // 键盘
    kb = lv_keyboard_create(lv_scr_act());
    lv_obj_set_size(kb, 500, 200);
    lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_keyboard_set_textarea(kb, pwd_ta);

    // 登录按钮
    login_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(login_btn, 100, 40);
    lv_obj_align_to(login_btn, pwd_ta, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_add_event_cb(login_btn, login_event_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t * login_label = lv_label_create(login_btn);
    lv_label_set_text(login_label, "登录");
    lv_obj_set_style_text_font(login_label, &chinese_ziku, 0);
    lv_obj_center(login_label);

    // 注册按钮
    register_btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(register_btn, 100, 40);
    lv_obj_align_to(register_btn, pwd_ta, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 20);
    lv_obj_add_event_cb(register_btn, register_event_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t * register_label = lv_label_create(register_btn);
    lv_label_set_text(register_label, "注册");
    lv_obj_set_style_text_font(register_label, &chinese_ziku, 0);
    lv_obj_center(register_label);
}

// ------------------------ 输入框事件 ------------------------
static void ta_event_cb1(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta        = lv_event_get_target(e);

    if(code == LV_EVENT_CLICKED || code == LV_EVENT_FOCUSED) {
        if(kb != NULL) {
            lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
            lv_keyboard_set_textarea(kb, ta);
        }
    } else if(code == LV_EVENT_READY) {
        // user = lv_textarea_get_text(text_ta);
        // pass = lv_textarea_get_text(pwd_ta);
        // LV_LOG_USER("账号: %s", user);
        // LV_LOG_USER("密码: %s", pass);
    }
}

// ------------------------ 点击背景隐藏键盘 ------------------------
static void bg_event_cb(lv_event_t * e)
{
    if(kb != NULL) {
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_keyboard_set_textarea(kb, NULL);
    }
}

// ------------------------ 登录按钮事件 ------------------------
static void login_event_cb(lv_event_t * e)
{
    // LV_LOG_USER("点击登录，进入主页面");
        user = lv_textarea_get_text(text_ta);
        pass = lv_textarea_get_text(pwd_ta);
        LV_LOG_USER("账号: %s", user);
        LV_LOG_USER("密码: %s", pass);
        ui_load_page(page_home); // 切换到主界面
}

// ------------------------ 注册按钮事件 ------------------------
static void register_event_cb(lv_event_t * e)
{
    // LV_LOG_USER("点击注册，返回注册页（暂未实现新页面）");
    ui_load_page(zhuce_home); // 这里也可以跳到别的页面
}

// ------------------------ 主界面 ------------------------
void page_home(void)
{
    lv_obj_t * label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "欢迎进入主页!");
    lv_obj_set_style_text_font(label, &chinese_ziku, 0);

    lv_obj_center(label);

    lv_obj_t * back_btn = lv_btn_create(lv_scr_act());
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_MID, 0, -30);
    lv_obj_add_event_cb(back_btn, lv_zhuce, LV_EVENT_CLICKED, NULL);

    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回登录页");
    lv_obj_set_style_text_font(back_label, &chinese_ziku, 0);
    lv_obj_center(back_label);
}

void zhuce_home(void)
{
    lv_obj_t * label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "注册页面!");
    lv_obj_set_style_text_font(label, &chinese_ziku, 0);

    lv_obj_center(label);

    lv_obj_t * back_btn = lv_btn_create(lv_scr_act());
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_MID, 0, -30);
    lv_obj_add_event_cb(back_btn, lv_zhuce, LV_EVENT_CLICKED, NULL);

    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回登录页");
    lv_obj_set_style_text_font(back_label, &chinese_ziku, 0);
    lv_obj_center(back_label);
}
